package com.lujieni.springbootio.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class FileService {


    @Async
    public void copyFile(String file){
        System.out.println(Thread.currentThread().getName());
        String fileFormat = file.substring(file.lastIndexOf("."));
        String newFileName = System.currentTimeMillis() + fileFormat;

        int buffSize = 1024 * 1024 * 4; // 4MB

        try (InputStream fileInputStream = new FileInputStream("d:/"+file);
             OutputStream fileOutputStream = new FileOutputStream("D:/springboot-io/"+newFileName);
             BufferedInputStream bi = new BufferedInputStream(fileInputStream, buffSize);
             BufferedOutputStream bo = new BufferedOutputStream(fileOutputStream, buffSize)
        ) {
            byte[] content = new byte[1024 * 1024];
            int len = -1;
            while((len = bi.read(content)) != -1){
                bo.write(content, 0, len);
            }
        } catch (IOException excepetion) {

        }
    }

}
